import { Controller, Post, Body, Session, HttpException } from '@nestjs/common';
import { UserService } from 'src/services/user/user.service';
import { User } from 'src/types/user.interface';

@Controller('user')
export class UserController {
    constructor(private readonly userService: UserService) {

    }
    @Post('generate-verification-code')
    async generateVerificationCode(
        @Body('phone') phone: string,
        @Session() session: Record<string, any>
    ) {
        //1.获得一个四位数字的验证码
        const code = Math.floor(1000 + Math.random() * 9000).toString();
        //把此手机号和验证码的关联对象保存在当前用户的会话里
        session.verificationCode = { phone, code }
        console.log(phone, code);
        return {
            success: true,
            message: 'generate verification code successfully',
            data: code
        };
    }
    @Post('check-verification-code')
    async checkVerificationCode(
        @Body('phone') phone: string,
        @Body('code') code: string,
        @Session() session: Record<string, any>
    ) {
        const { verificationCode } = session;
        if (verificationCode && verificationCode.code === code && verificationCode.phone === phone) {
            return {
                success: true,
                message: 'success',
                data: 'valid'
            };
        } else {
            return {
                success: false,
                message: 'failed',
                data: 'invalid'
            };
        }

    }
    @Post('login-with-verification-code')
    async loginWithVerficationCode(
        @Body('phone') phone: string,
        @Body('code') code: string,
        @Session() session: Record<string, any>
    ) {
        const { verificationCode } = session;
        if (verificationCode && verificationCode.code === code && verificationCode.phone === phone) {
            const user = await this.userService.findByPhone(phone)
            if (!user) {
                throw new HttpException('No user associated with this phone number', 404)
            }
            return {
                success: true,
                message: 'success',
                data: user
            };
        } else {
            return {
                success: false,
                message: 'failed',
                data: 'invalid'
            };
        }

    }
    @Post('login-with-password')
    async loginWithPassword(
        @Body('username') username: string,
        @Body('password') password: string,
    ) {
        const user = await this.userService.loginWithPassword(username,password)
        if (!user) {
            throw new HttpException('invalid username or password', 404)
        }
        return {
            success: true,
            message: 'user login successfully',
            data: user
        }

    }
    @Post('register')
    async register(
        @Body() createUserDto: User,
    ) {
        const data = await this.userService.register(createUserDto)
        return {
            success: true,
            message: 'register successfully',
            data
        };
    }
}
